package com.example.composedemo

import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.ScrollableColumn
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composedemo.ui.ComposeDemoTheme

// https://www.jianshu.com/p/968e2339ee5f

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {

                    ScrollableColumn() {
                        TextWeight("Jetpack Compose")

                        Greeting("Jetpack Compose", this@MainActivity)
                    }
                }
            }
        }

        val packageManager = packageManager
        val intent = Intent(Intent.ACTION_MAIN)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)

        // 这里面的值是你要跳转app的包名，你跳转的清单文件里的package名
//        intent = packageManager.getLaunchIntentForPackage("com.shidian.record")
        val c = ComponentName(
            "com.shidian.record",
            "com.shidian.record.message.activity.LoginActivity"
        )
        intent.setComponent(c)
        startActivity(intent)
    }

    override fun onResume() {
        super.onResume()


    }

    override fun onBackPressed() {

    }
}



/**
 * 字体字重
 */
@Composable
fun TextWeight(name: String) {

    Column(modifier = Modifier.padding(20.dp)) {

        // FontWeight字重
        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            fontWeight = FontWeight.Normal,
        )

        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            fontWeight = FontWeight.Medium,
        )

        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            fontWeight = FontWeight.Bold,
        )

        // letterSpacing 字符间距
        Spacer(modifier = Modifier.height(20.dp))
        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            fontWeight = FontWeight.Medium,
            letterSpacing = 4.sp
        )

        // TextDecoration
        Spacer(modifier = Modifier.height(20.dp))
        // 删除线
        Text(
            text = "Hello $name!",
            color = Color.Gray,
            fontSize = 26.sp,
            fontStyle = FontStyle.Normal,
            textDecoration = TextDecoration.LineThrough
        )

        // 下划线
        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            textDecoration = TextDecoration.Underline
        )

        // fontStyle
        Spacer(modifier = Modifier.height(20.dp))
        // 斜体
        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            fontStyle = FontStyle.Italic
        )

        // overflow 字体溢出
        Spacer(modifier = Modifier.height(20.dp))
        // 溢出部分直接截取
        Text(
            text = "Hello Hello $name!",
            color = Color.Gray,
            fontSize = 26.sp,
            overflow = TextOverflow.Clip,
            maxLines = 1
        )
        // 省略号
        Text(
            text = "Hello Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
        )

        // 自动换行
        Spacer(modifier = Modifier.height(20.dp))
        Text(
            text = "Hello Hello $name!",
            color = Color.Gray,
            fontSize = 26.sp,
            softWrap = false // 户换行
        )

        Text(
            text = "Hello Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            softWrap = true // 换行
        )

        // textAlign 对其方式
        Spacer(modifier = Modifier.height(20.dp))
        Text(
            text = name,
            color = Color.Black,
            fontSize = 26.sp,
            textAlign = TextAlign.Left, // TextAlign.Start
            modifier = Modifier.fillMaxWidth()
        )

        Text(
            text = name,
            color = Color.Black,
            fontSize = 26.sp,
            textAlign = TextAlign.Center,
            modifier = Modifier
                .fillMaxWidth()
                .background(color = Color.Cyan)
                .padding(vertical = 15.dp)
        )

        Text(
            text = name,
            color = Color.Black,
            fontSize = 26.sp,
            textAlign = TextAlign.Right, // TextAlign.End
            modifier = Modifier.fillMaxWidth(),
            style = TextStyle(background = Color.Red)
        )

        Text(
            text = name,
            color = Color.Red, // 会覆盖Style中的
            fontSize = 14.sp,
            modifier = Modifier.fillMaxWidth(),
            style = TextStyle(
                color = Color.Black,
                fontSize = 26.sp,
                textAlign = TextAlign.Right, // TextAlign.End
            )
        )


//        val test = remember { mutableStateOf("") }
        val test = mutableStateOf("")

        TextField(
            value = test.value,
            onValueChange = {
                test.value = it
            },
            placeholder = {
                Text(text = "请输入用户名")
            },
            backgroundColor = Color.Transparent,
        )


//

//        TextButton(onClick = { /*TODO*/ }) {
//
//        }

//        LazyColumn(content = { /*TODO*/ })
    }
}

@Preview(showBackground = true)
@Composable
fun TextWeightPreview() {
    ComposeDemoTheme {
        TextWeight("Jetpack Compose")
    }
}

@Composable
fun Greeting(name: String, activity: AppCompatActivity?) {
    Column(modifier = Modifier.padding(20.dp)) {
        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            softWrap = true,
            overflow = TextOverflow.Ellipsis,
            fontStyle = FontStyle.Normal,
            fontWeight = FontWeight.Thin
        )

        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            softWrap = true,
            overflow = TextOverflow.Ellipsis,
            fontStyle = FontStyle.Normal,
            fontWeight = FontWeight.Normal,
            fontFamily = FontFamily.SansSerif
        )

        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            softWrap = true,
            overflow = TextOverflow.Ellipsis,
            fontStyle = FontStyle.Normal,
            fontWeight = FontWeight.Medium,
            letterSpacing = 4.sp
        )

        Text(
            text = "Hello $name!",
            color = Color.Black,
            fontSize = 26.sp,
            softWrap = true,
            overflow = TextOverflow.Ellipsis,
            fontWeight = FontWeight.Bold
        )

        Spacer(modifier = Modifier.height(10.dp))

        Image(
            bitmap = imageResource(id = R.drawable.header),
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .preferredHeight(200.dp)
                .fillMaxWidth()
                .clickable(onClick = { /*TODO*/ })
//                .border()
                .clip(RoundedCornerShape(6.dp)), // 圆角

        )
        
        Card() {
            
        }


        Spacer(modifier = Modifier.height(10.dp))

        Button(
            onClick = {
                Log.e("this", "点我了！ 点我了！")
//                activity?.startActivity(Intent(activity, ConstraintActivity::class.java))

                val packageManager = activity!!.packageManager
                var intent: Intent? = Intent()
                // 这里面的值是你要跳转app的包名，你跳转的清单文件里的package名
                intent = packageManager.getLaunchIntentForPackage("com.android.settings")
                activity!!.startActivity(intent)
            },
            colors = ButtonConstants.defaultButtonColors(backgroundColor = Color.Blue),
//            modifier = Modifier
        ) {
            Text(
                text = "点我啊",
                color = Color.White,
                textAlign = TextAlign.Center
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeDemoTheme {
        Greeting("Jetpack Compose", null)
    }
}